<?php
//咨询控制器
class Controller_App_Chat extends Controller_App_Template
{
    public function __construct ($req, $res)
    {
        $this->template = "smarty:app/chat/chat";
        parent::__construct($req, $res);
    }

	//咨询信息展示
    public function action_index ()
    {
        $chatList = App_Chat::infoList($this->_user);
        $this->template->set_filename('smarty:app/chat/chat');
        $this->template->set('chatList', $chatList);
    }

    public function action_refresh()
    {
        $chatList = App_Chat::infoList($this->_user);
        $html     = View::factory('smarty:app/chat/records')
            ->set('chatList', $chatList)
            ->render();

        $ret = array('status' => 0, 'html' => $html);
        echo json_encode($ret);exit;
    }

	//接受并处理提交到服务器的信息
    public function action_api ()
    {
        $img = Arr::get($this->_post, 'img');
        $txt = Arr::get($this->_post, 'txt');
        $toUid = Arr::get($this->_post, 'touid');

        if (! empty($img))
            $img = "<img src=\"$img\">";

        if (! empty($txt))
        {
            $txt = htmlentities($txt);
            $txt = nl2br($txt);
        }

        if (empty($txt) && empty($img))
            exit;

        $txt   = empty($img) ? $txt : $img;
        if ($toUid > 0)
        {
            $user  = User::getUserFromUid($toUid);
            $msgId = App_Chat::add($user, array('message' => $txt, 'fromUser' => 0));
        }
        else
        {
            $msgId = App_Chat::add($this->_user, array('message' => $txt, 'fromUser' => 1));
        }

        $html  = App_Chat::buildMessageHtml($msgId);
            
        echo json_encode(array(
            'status' => 0,
            'html'   => $html,
        ));

        exit;
    }

    public function action_list ()
    {
        $addrList = App_Address::infoList($this->_user);
        $this->template->set_filename('smarty:app/address/list');
        $this->template->set('addressList', $addrList);
    }

    public function action_add ()
    {
        if (empty($this->_post))
        {
            $this->template->set_filename('smarty:app/address/edit');
            $this->template->set('optionsProvice', App_Region::optionsByParentId());
            
        }
        else
        {
            App_Address::add($this->_user, $this->_post);
            $this->request->redirect('/address/list');
        }
    }

    public function action_edit ($nid)
    {
        if (empty($this->_post))
        {
            $addrInfo = App_Address::detail($nid);
            $this->template->set_filename('smarty:app/address/edit');
            $this->template->set('optionsProvice', App_Region::optionsByParentId());
            $this->template->set('optionsCity', App_Region::optionsByParentId($addrInfo['proviceId']));
            $this->template->set('optionsCountry', App_Region::optionsByParentId($addrInfo['cityId']));
            $this->template->set('address', $addrInfo);
        }
        else
        {
            App_Address::edit($this->_user, $nid, $this->_post);
            $this->request->redirect('/address/list');
        }
    }

    public function action_del ($nid)
    {
        App_Address::remove($this->_user, $nid);
        $this->request->redirect('/address/list');
    }
}
